import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_displaymode/flutter_displaymode.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'common/apis/homepage.dart';
import 'common/apis/network/net_cache.dart';
import 'common/store/config_store.dart';
import 'common/store/store_keys.dart';
import 'common/store/user_store.dart';
import 'common/utils/index.dart';
import 'package:device_info_plus/device_info_plus.dart';

import 'common/utils/web_rtc_channel.dart';
import 'dart:io';

/// 全局静态数据
class Global {
  // 网络缓存对象
  static NetCache netCache = NetCache();

  // 是否为release版
  static bool get isRelease => const bool.fromEnvironment("dart.vm.product");

  // 推送管理类
  // static AppPush appPush = AppPush();
  // websocket管理类
  static WebSocketChannel websocket = WebSocketChannel();
  static WebRtcChannel webRtcChannel = WebRtcChannel();

  static Future init() async {
    //仅在需要在调用runApp之前初始化绑定的情况下才需要调用此方法。
    // WidgetsFlutterBinding.ensureInitialized();
    // 初始化本地存储
    await GetStorage.init();

    // loading toast
    EasyLoading.init();

    // 初始化推送管理类
    // appPush.init();

    WidgetsFlutterBinding.ensureInitialized();
    await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

    setSystemUi();

    //获取设备信息
    final deviceInfoPlugin = DeviceInfoPlugin();
    final deviceInfo = await deviceInfoPlugin.deviceInfo;
    final allInfo = deviceInfo.data;
    // 应用版本信息
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    ConfigStore().appVerson = packageInfo.version;
    ConfigStore().deviceName =
        Platform.isIOS ? allInfo['name'] : allInfo['model'];
    //没有mobileToken的话去初始化
    if (UserStore().mobileToken == null) {
      HomepageAPI.mobileNew({});
    }
    // 判断是否是第一次打开app
    if (GetStorage().read(STORAGE_DEVICE_FIRST_OPEN_KEY) == null) {
      ConfigStore().isFirstOpen = true;
    }
    if (kDebugMode) {
      print('初始化全局类');
    }
  }

  // 初始化安卓系统UI
  static void setSystemUi() async {
    if (GetPlatform.isAndroid) {
      SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarBrightness: Brightness.light,
        statusBarIconBrightness: Brightness.light,
        systemNavigationBarDividerColor: Colors.transparent,
        systemNavigationBarColor: Colors.white,
        systemNavigationBarIconBrightness: Brightness.dark,
      );
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
      SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
          overlays: [SystemUiOverlay.top]);

      // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
      //     overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]);
      await FlutterDisplayMode.setHighRefreshRate();
    }
  }

  /// 获取iPhone设备型号
  static String getIphoneModel(String iMachine) {
    // iPhone
    if (iMachine == "iPhone1,1") return "iPhone 2G";
    if (iMachine == "iPhone1,2") return "iPhone 3G";
    if (iMachine == "iPhone2,1") return "iPhone 3GS";
    if (iMachine == "iPhone3,1") return "iPhone 4";
    if (iMachine == "iPhone3,2") return "iPhone 4";
    if (iMachine == "iPhone3,3") return "iPhone 4";
    if (iMachine == "iPhone4,1") return "iPhone 4S";
    if (iMachine == "iPhone5,1") return "iPhone 5";
    if (iMachine == "iPhone5,2") return "iPhone 5";
    if (iMachine == "iPhone5,3") return "iPhone 5c";
    if (iMachine == "iPhone5,4") return "iPhone 5c";
    if (iMachine == "iPhone6,1") return "iPhone 5s";
    if (iMachine == "iPhone6,2") return "iPhone 5s";
    if (iMachine == "iPhone7,1") return "iPhone 6 Plus";
    if (iMachine == "iPhone7,2") return "iPhone 6";
    if (iMachine == "iPhone8,1") return "iPhone 6s";
    if (iMachine == "iPhone8,2") return "iPhone 6s Plus";
    if (iMachine == "iPhone8,4") return "iPhone SE 1";
    if (iMachine == "iPhone9,1") return "iPhone 7";
    if (iMachine == "iPhone9,3") return "iPhone 7";
    if (iMachine == "iPhone9,2") return "iPhone 7 Plus";
    if (iMachine == "iPhone9,4") return "iPhone 7 Plus";
    if (iMachine == "iPhone10,1") return "iPhone 8";
    if (iMachine == "iPhone10,4") return "iPhone 8";
    if (iMachine == "iPhone10,2") return "iPhone 8 Plus";
    if (iMachine == "iPhone10,5") return "iPhone 8 Plus";
    if (iMachine == "iPhone10,3") return "iPhone X";
    if (iMachine == "iPhone10,6") return "iPhone X";
    if (iMachine == "iPhone11,2") return "iPhone XS";
    if (iMachine == "iPhone11,4") return "iPhone XS MAX";
    if (iMachine == "iPhone11,6") return "iPhone XS MAX";
    if (iMachine == "iPhone11,8") return "iPhone XR";
    if (iMachine == "iPhone12,1") return "iPhone 11";
    if (iMachine == "iPhone12,3") return "iPhone 11 Pro";
    if (iMachine == "iPhone12,5") return "iPhone 11 Pro Max";
    if (iMachine == "iPhone12,8") return "iPhone SE 2";
    if (iMachine == "iPhone13,1") return "iPhone 12 mini";
    if (iMachine == "iPhone13,2") return "iPhone 12";
    if (iMachine == "iPhone13,3") return "iPhone 12 Pro";
    if (iMachine == "iPhone13,4") return "iPhone 12 Pro Max";
    if (iMachine == "iPhone14,4") return "iPhone 13 mini";
    if (iMachine == "iPhone14,5") return "iPhone 13";
    if (iMachine == "iPhone14,2") return "iPhone 13 Pro";
    if (iMachine == "iPhone14,3") return "iPhone 13 Pro Max";
    if (iMachine == "iPhone14,6") return "iPhone SE 3";
    if (iMachine == "iPhone14,7") return "iPhone 14";
    if (iMachine == "iPhone14,8") return "iPhone 14 Plus";
    if (iMachine == "iPhone15,2") return "iPhone 14 Pro";
    if (iMachine == "iPhone15,3") return "iPhone 14 Pro Max";
    return iMachine;
  }
}
